package com.scalar.chiptrack.jobs;

import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.StringUtils;

import java.util.Calendar;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;

/**
 * User: Sujan Kumar Suppala
 * Date: Feb 23, 2009
 * Time: 11:21:07 AM
 */
public class ReportConfiguration
{
    private int reportId;
    private String reportName;
    private String reportClassName;
    private String reportMethodName;
    private Object[] args;
    private Class[] paramTypes;
    private String reportMethodReturnType; //FILE_PATH, WORK_BOOK, CONTENT_STRING
    private String reportFileNameWithExtn;
    private String subject;
    private String filenameTimestampFormat; //BLANK


    public int getReportId() {
        return reportId;
    }

    public void setReportId(int reportId) {
        this.reportId = reportId;
    }

    public String getReportName() {
        return reportName;
    }

    public void setReportName(String reportName) {
        this.reportName = reportName;
    }

    public String getReportClassName() {
        return reportClassName;
    }

    public void setReportClassName(String reportClassName) {
        this.reportClassName = reportClassName;
    }

    public String getReportMethodName() {
        return reportMethodName;
    }

    public void setReportMethodName(String reportMethodName) {
        this.reportMethodName = reportMethodName;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public Class[] getParamTypes() {
        return paramTypes;
    }

    public void setParamTypes(Class[] paramTypes) {
        this.paramTypes = paramTypes;
    }

    public String getReportMethodReturnType() {
        return reportMethodReturnType;
    }

    public void setReportMethodReturnType(String reportMethodReturnType) {
        this.reportMethodReturnType = reportMethodReturnType;
    }

    public String getReportFileNameWithExtn() {
        return reportFileNameWithExtn;
    }

    public void setReportFileNameWithExtn(String reportFileNameWithExtn) {
        this.reportFileNameWithExtn = reportFileNameWithExtn;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getFilenameTimestampFormat() {
        return filenameTimestampFormat;
    }

    public void setFilenameTimestampFormat(String filenameTimestampFormat) {
        this.filenameTimestampFormat = filenameTimestampFormat;
    }

    public Object[] getFilledArgs( String toEmailIds )
    {
        Object[] filledArgs = null;
        Calendar calendar = Calendar.getInstance();
        if ( args != null )
        {
            filledArgs = new Object[args.length];
            for ( int i=0; i < args.length; i++ )
            {
                if ( args[i] instanceof String )
                {
                    filledArgs[i] = getFilledValue( (String)args[i], calendar, toEmailIds );
                }
                else if ( args[i] instanceof Map )
                {
                    Map map = (Map)args[i];
                    Map filledMap = new HashMap();
                    Iterator itr = map.keySet().iterator();
                    while ( itr.hasNext() )
                    {
                        Object key = itr.next();
                        Object value = map.get( key );
                        if ( value instanceof String )
                        {
                            value = getFilledValue( (String)value, calendar, toEmailIds );
                        }
                        filledMap.put( key, value );
                    }
                    filledArgs[i] = filledMap;
                }
                else
                {
                    filledArgs[i] = args[i];
                }
            }
        }
        return filledArgs;
    }
    private String getFilledValue( String str, Calendar calendar ,String toEmailIds )
    {
        String filledValue = str;
        if ( filledValue != null )
        {
            if ( filledValue.indexOf( "<CURRENT_MONTH_START_DATE>" ) != -1 )
            {
                filledValue = filledValue.replaceAll("<CURRENT_MONTH_START_DATE>", DateUtil.getCurrentMonth(calendar) + "/01/" + DateUtil.getCurrentYear(calendar) );
            }
            if ( filledValue.indexOf( "<CURRENT_YEAR_START_DATE>" ) != -1 )
            {
                filledValue = filledValue.replaceAll("<CURRENT_YEAR_START_DATE>", "01/01/" + DateUtil.getCurrentYear(calendar) );
            }
            if ( filledValue.indexOf( "<CURRENT_QUATER_START_DATE>" ) != -1 )
            {
                filledValue = filledValue.replaceAll("<CURRENT_QUATER_START_DATE>", (DateUtil.getStartMonthOfQtr(DateUtil.getCurrentQtrNo()) > 9 ? DateUtil.getStartMonthOfQtr(DateUtil.getCurrentQtrNo()) : "0"+DateUtil.getStartMonthOfQtr(DateUtil.getCurrentQtrNo()) ) + "/01/" + DateUtil.getCurrentYear(calendar) );
            }
            if ( filledValue.indexOf( "<CURRENT_DATE>" ) != -1 )
            {
                filledValue = filledValue.replaceAll("<CURRENT_DATE>", StringUtils.formatDateToString(calendar.getTime()) );
            }
            if ( filledValue.indexOf( "<TO_EMAILS>" ) != -1 )
            {
                filledValue = filledValue.replaceAll("<TO_EMAILS>", toEmailIds );
            }

        }
        return filledValue;
    }
}
